package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct/")
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    /**
     * 分页查询附件列表（根据contractProductId货物id，查询）
     *  参数：
     *     contractProductId:货物id
     *     contractId:购销合同id
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size,
            String contractProductId,String contractId) {
        //1、根据货物id，分页查询附件列表
        ExtCproductExample example = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = example.createCriteria();
        criteria.andContractProductIdEqualTo(contractProductId);
        PageInfo info = extCproductService.findAll(example, page, size);
        request.setAttribute("page",info);

        //2、查询所有ctype=“附件”的生产厂家
        FactoryExample fe = new FactoryExample();
        FactoryExample.Criteria fc = fe.createCriteria();
        fc.andCtypeEqualTo("附件");
        List<Factory> list = factoryService.findAll(fe);
        request.setAttribute("factoryList",list);

        //3、为了方便保存，将货物id和合同id，存入request域
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);
        return "cargo/extc/extc-list";
    }

    /**
     * 根据id查询
     *  1、根据id查询附件
     *  2、查询所有ctype=“附件”的生产厂家
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //1、根据id查询附件
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);
        //2、查询所有ctype=“附件”的生产厂家
        FactoryExample fe = new FactoryExample();
        FactoryExample.Criteria fc = fe.createCriteria();
        fc.andCtypeEqualTo("附件");
        List<Factory> list = factoryService.findAll(fe);
        request.setAttribute("factoryList",list);
        return "cargo/extc/extc-update";
    }

    /**
     * 新增或者修改
     *  ExtCproduct :
     *    contractId: 所属合同id
     *    contractProductId：所属货物id
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct ext) {
        ext.setCompanyId(getLoginCompanyId());
        ext.setCompanyName(getLoginCompanyName());
        if(StringUtils.isEmpty(ext.getId())) {
            extCproductService.save(ext);
        }else{
            extCproductService.update(ext);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+ext.getContractId() + "&contractProductId="+ext.getContractProductId();
    }

    /**
     * 参数：
     *    id：附件id（业务删除）
     *    contractId：合同id （页面跳转）
     *    contractProductId：货物id（页面跳转）
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId ) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }
}
